package com.ss;

import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.lang.Object;


public class Monte_Carlo {
public int Monte_Carlo_m(String symbol)
{
Calendar cal = Calendar.getInstance();
Dbconnection db = new Dbconnection();
Connection conn=db.dbConnect("jdbc:sqlserver://localhost:1433;", "sa","");
int k=-1;
int period =20;
float stoc_val[]={};
Dbconnection db_getdata = new Dbconnection();
for (int j=0;j<period;j++)
{
DateFormat formatter ; 
formatter = new SimpleDateFormat("dd-MMM-yy");
             String s = formatter.format(cal.DATE);
String dt = s;  // Start date
try {
cal.setTime(formatter.parse(dt));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.add(Calendar.DATE, k);  // number of days to add
dt = formatter.format(cal.getTime());  // dt is now the new date

stoc_val[j] = db_getdata.getdata(conn, symbol, dt);
k=k-1;
}
//calculation of  drift and volatility
float drift_s = 0, volatility_s=0, volatility, drift,drift_mean,z[] ={};
for (int m=0;m<period;m++)
{
drift_s += stoc_val[m];
}
drift = drift_s/period;
for (int m=0;m<period;m++)
{
volatility_s += Math.pow(((stoc_val[m])-drift),2);
}
volatility = (float) (Math.sqrt(volatility_s))/period;
drift_mean = drift-(float)(0.5)*volatility*volatility;
Random random = new Random();
float ans[]={};
float f_ans=0;
for (int e=0;e<1000;e++)
{
z[e]= (random.nextFloat()*6-3);
ans[e]= drift_mean + volatility*z[e];
f_ans += ans[e];
}
if (f_ans>0){return 1;}
else if (f_ans<0){return -1;}
else if (f_ans==0){return 0;}
   return 2;
}

}

